package com.hopu.blb_erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.entity.TDrive;
import com.hopu.blb_erp.service.DriveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api
@RestController
//
public class DriveController {
    @Autowired
    private DriveService driveService;

    @ApiOperation("查询所有的驾驶员,分页")
    @GetMapping("/drivers/{current}/{pageSize}")
    public ResponseEntity<PageEntity> findPage(@PathVariable("current")Integer current, @PathVariable("pageSize")Integer pageSize,
                                               String driverName,String driverLicense){
        PageEntity<TDrive> pages = driveService.findDriverPages(current, pageSize,driverName,driverLicense);
        return ResponseEntity.ok(pages);
    }

    @ApiOperation("查询所有驾驶员")
    @GetMapping("/drivers")
    public ResponseEntity<List<TDrive>> findAllDrive(){
        List<TDrive> drivers = driveService.findAllDrive();
        return ResponseEntity.ok(drivers);
    }

    @ApiOperation("对驾驶员进行更新")
    @PostMapping("/driver")
    public ResponseEntity<String> maDrive(@RequestBody TDrive drive){
        driveService.saveOrUpdate(drive);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("根据id进行删除驾驶员")
    @DeleteMapping("/driver/{id}")
    public ResponseEntity<String> removeDrive(@PathVariable Integer id){
        driveService.removeById(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("根据车辆id查询所有驾驶员")
    @GetMapping("/driver")
    public ResponseEntity<List<TDrive>> findDriveById(Integer vehicleId){
        List<TDrive> drivers = driveService.list(new QueryWrapper<TDrive>().lambda().eq(TDrive::getVehicleId, vehicleId));
        return ResponseEntity.ok(drivers);
    }

    @ApiOperation("根据驾驶员id查询驾驶员")
    @GetMapping("/driverId")
    public ResponseEntity<TDrive> findById(Integer driverId){
        TDrive driver = driveService.getById(driverId);
        return ResponseEntity.ok(driver);
    }
}
